/**
 * DataParser.java
 * @authors: Connor Gordon, Difan Zhao, Kathleen Lan, Patrick Royal
 */

package io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import model.Element;

/**
 * 
 * @author Group 8
 */
public abstract class DataParser {

    private static final int URL_MAX_DISPLAY = 26;
    private static final int BUFFER_SIZE = 153600;
    private final String myFileDirectory = "src/resources/";
    //private final String myExt = "";

    private String myName;
    private String myId;
    private File myFile;

    /**
     * Default parser for data that comes from a file.
     * @param name name of the data set
     * @param id abbrev of the dataSet
     * @param f name of the file
     */
    public DataParser (String name, String id, File f) {
        myName = name;
        myId = id;
        myFile = f;
    }

    /**
     * Default parser for data that comes from a url.
     * @param name name of the data set
     * @param id abbrev for the data set
     * @param url the source for the data
     * @param ext data type extension
     */
    public DataParser (String name, String id, String url, String ext) {
        myName = name;
        myId = id;
        myFile = convertURLtoFile(url, ext);
    }

    /**
     * To be implemented by CSV and XLS classes.
     */
    public abstract Element parseData ();

    /**
     * Returns file that is being parsed
     * @return file Parser is reading
     */
    protected File getFile () {
        return myFile;
    }

    /**
     * Return data set name.
     */
    public String getName () {
        return myName;
    }

    /**
     * Return Element ID
     */
    public String getId() {
        return myId;
    }

    /**
     * Extracts the file from a given URL
     * 
     * @param urlString web address of the file in which the element resides
     */
    private File convertURLtoFile (String urlString, String ext) {
        try {
            URL url = new URL(urlString);
            url.openConnection();
            InputStream reader = url.openStream();

            String outputFile = String.format("%sloadin%s", myFileDirectory, ext);
            FileOutputStream writer = new FileOutputStream(outputFile);
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = 0;

            while ((bytesRead = reader.read(buffer)) > 0) {
                writer.write(buffer, 0, bytesRead);
                buffer = new byte[BUFFER_SIZE];
            }

            System.out.println("Download Complete: " + myId + " from " +
                    urlString.substring(0, URL_MAX_DISPLAY) + "...");
            writer.close();
            reader.close();
            return new File(outputFile);
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
